package com.hitrust.gs.action.productTiptopStock;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;

public class GSdownloadMouldAction extends GSBaseAction {
	public String execute(GSBaseForm aForm) throws Exception {
		String path = aForm.getRequest().getServletPath();
		String excelPath = null;
		String filePath = null;
		if ("/A3000012.do".equals(path)) {
			excelPath = "/back/file/templet_excel.xls";
			filePath = servlet.getServletContext().getRealPath("/back/file/templet_excel.xls");
		}else if ("/A3000034.do".equals(path)) {
			excelPath = "/back/file/notpurchase_excel.xls";
			filePath = servlet.getServletContext().getRealPath("/back/file/notpurchase_excel.xls");
		}else if ("/A3000052.do".equals(path)) {
			excelPath = "/back/file/productMapOld1_excel.xls";
			filePath = servlet.getServletContext().getRealPath("/back/file/productMapOld1_excel.xls");
		}
		
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		OutputStream fos = null;
		InputStream fis = null;
		
		File uploadFile = new File(filePath);
		fis = new FileInputStream(uploadFile);
		bis = new BufferedInputStream(fis);
		fos = aForm.getResponse().getOutputStream();
		bos = new BufferedOutputStream(fos);
		
		aForm.getResponse().setHeader("Content-disposition", "attachment;filename="+URLEncoder.encode(excelPath,"utf-8"));
		int bytesRead = 0;
		byte[] buffer = new byte[1024];
		while((bytesRead = bis.read(buffer,0,1024))!=-1){
			bos.write(buffer,0,bytesRead);
		}
		bos.flush();
		fis.close();
		bis.close();
		fos.close();
		bos.close();
		return null;
	}
}

